app.controller("ucloudCtr", ["$scope", "$http", "$filter", "$rootScope", "$stateParams", "lodash",
    function($scope, $http, $filter, $rootScope, $stateParams, lodash) {
        $scope.info = {
            areaList: config_area_list,
            sltAreaName: lodash.first(config_area_list).areaName,
            messageData: [],
            appList: [],
            sltAppItem: "",
            queueList: [],
            sltQueueItem: "",
            tabList: [
                {"name": "输入", "type": "in"},
                {"name": "返回值", "type": "out"},
            ],
            jsonData: "",
        };

        getAppList();

        // 切换区域
        $scope.switchArea = function(area) {
            console.log(area);
            $scope.info.sltAreaName = area.areaName;
        };

        // 点击单选按钮
        $scope.clickRadio = function(type) {
            console.log(type);
            if(type == "msg") {
                getAppList();
            } else {
                $rootScope.toasterWarningInfo("暂无事件数据！");
            }
        };

        // 获取应用数据
        function getAppList() {
            var url = "../data/cloudLink.json";
            $http.get(url).success(function(result) {
                if(result.code == "0") {
                    console.log(result.data);
                    // 设置应用数据
                    var filterAppDataArray = new Array();
                    angular.forEach(lodash.pluck(result.data, "appId"), function(res) {
                        var filterIndex = lodash.findIndex(filterAppDataArray, function(filter) {
                            return res == filter.crtname;
                        });
                        if(filterIndex == -1) {
                            filterAppDataArray.push({"crtname": res});
                        }
                    });
                    $scope.info.appList = filterAppDataArray;
                    $scope.info.messageData = result.data;
                } else {
                    $rootScope.toasterErrorInfo("获取应用数据失败！");
                }
            });
        };

        // 切换应用，获取队列数据
        $scope.selectAppFun = function(item) {
            var queueDataArray = new Array();
            angular.forEach($scope.info.messageData, function(messageItem) {
                if(messageItem.appId == item.crtname) {
                    messageItem.crtname = messageItem.eventName;
                    queueDataArray.push(messageItem);
                }
            });
            $scope.info.queueList = queueDataArray;
        };

        // 切换队列，获取结果
        $scope.selectQueueFun = function(item) {
            $scope.info.jsonData = JSON.parse(item.parameter);
        };

        // 切换tab页
        $scope.tabClick = function(tab) {
            console.log(tab);
        };

        // 发送消息
        $scope.sendClick = function() {
            if(!$scope.info.jsonData) {
                $rootScope.toasterWarningInfo("发送消息不能为空！");
                return false;
            }
            $rootScope.toasterSuccessInfo("消息发送成功！请等待返回结果！");
        }

    }
]);
